﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace WordTool
{
    public partial class FrmMain : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private WordModelContainer modelContext;
        private List<Word> selectedWord;

        delegate void UpdateWord(Word w);

        private int curIndex;
        private Word curWord;

        private FrmSetting frmSetting;
        private AppSetting appSetting;

        private WMPLib.WindowsMediaPlayer windowsMediaPlayer;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void mniExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            appSetting = new AppSetting();
            ApplySetting();
            modelContext = new WordModelContainer();
            selectedWord = modelContext.Words.Where((w) => w.selected == true).ToList();
            curIndex = 0;
            windowsMediaPlayer = new WMPLib.WindowsMediaPlayer();
            if (selectedWord.Count > 0)
            {
                bgWorker.RunWorkerAsync();
            }

        }

        private void ApplySetting()
        {
            this.Opacity = appSetting.Opacity;
        }
        private void StudyWords()
        {
            curWord = selectedWord[curIndex];
            Update(curWord);
            if (appSetting.AutoPlayWord)
            {
                playSingle(curWord.word1);
            }
            if (++curIndex == selectedWord.Count) curIndex = 0;

        }

        private void UpdateUI(Word w)
        {
            lblword.Text = w.word1;
            lblphone.Text = w.phonetic;
            lblmeaning.Text = w.meaning;
            this.Hide();
            this.Show();
        }

        private void Update(Word w)
        {
            this.Invoke(new UpdateWord(UpdateUI), new object[] { w});
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                StudyWords();
                Thread.Sleep(appSetting.SleepTime * 1000);
            }
        }

        private void btnPron_Click(object sender, EventArgs e)
        {
            playSingle(curWord.word1);
        }

        private void playSingle(string word)
        {

            string file = Directory.GetCurrentDirectory() + "\\Speech\\default\\" + word[0] + "\\" + word + ".mp3";

            windowsMediaPlayer.URL = file;
            windowsMediaPlayer.controls.play();
        }

        private void settingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (frmSetting == null)
            {
                frmSetting = new FrmSetting();
                frmSetting.setting = appSetting;
            }
            frmSetting.ShowDialog();
        }

        private void wordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmWord frmWord = new FrmWord();
            frmWord.modelContext = modelContext;
            frmWord.init();
            frmWord.ShowDialog();
        }

        
    }
}
